package com.yidu.hbatsft.action;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yidu.hbatsft.domain.*;
import com.yidu.hbatsft.service.AtsEmpAthleteService;
import com.yidu.hbatsft.service.impl.AtsEmpAthleteImpl;
import com.yidu.privilege.vo.UserVo;
import com.yidu.utils.Message;
import com.yidu.utils.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/AtsEmpAthleteAction")
public class AtsEmpAthleteController {
    @Resource
    AtsEmpAthleteService service;
    /**
     *查询所有的方法
     * @return
     * 张雨虹
     */
    @RequestMapping("findAll")
    @ResponseBody
    public List<AtsEmpAthlete> findAll(){
        List<AtsEmpAthlete> list=service.findAllx();
        return list;
    }
    @RequestMapping("findbycoachId")
    @ResponseBody
    public List<AtsEmpAthlete> findbycoachId(Integer Id){
        System.out.println("教练id"+Id);
        List<AtsEmpAthlete> list=service.findbycoachId(Id);
        return list;
    }
    @RequestMapping("findLogin")
    @ResponseBody
    public Message findLogin(HttpSession session){
        Message msg = new Message();
        UserVo userInfo = (UserVo) session.getAttribute("manager");
        if(userInfo == null){
            msg.setNum(0);
            msg.setMsg("请登陆");
        }
        return  msg;
    }
    /**
     * 查询全部
     */
    @RequestMapping("findAllX")
    @ResponseBody
    public Map<String,Object> findAllX(HttpSession session,String athleteName,@RequestParam("page") Integer pageNum, @RequestParam("limit") Integer pageSize){
        //根据教练ID查询运动员
        Map<String,Object> map = new HashMap<String,Object>();

       List<AtsEmpAthlete> ata =  service.findAllX(pageNum,pageSize,athleteName);

        int cout = service.count();

        map.put("code",0);
        map.put("msg","");
        map.put("count",cout);
        map.put("data",ata);

        return map;
    }
    /**
     * 根据ID查询
     */
    @RequestMapping("findById")
    @ResponseBody
    public AtsEmpAthleteVo findById(Integer id){
        //System.out.println("进入根据ID查询的方法");
        System.out.println(id);
        if(id != null){
            //System.out.println("进入查询ID方法"+id);
            AtsEmpAthleteVo ats = service.findByid(id);
            System.out.println("根据ID加载的数据："+ats.getUsPhoto());
            //System.out.println("运动员名字："+aea.getAthleteName());
            return ats;
        }
        return null;
    }
    /**
     * 下拉框加载教练员
     */
    @RequestMapping("findAllCoach")
    @ResponseBody
    public List<AtsEmpCoach> findAllCoach(){
        //System.out.println("进入查询教练员方法");
        List<AtsEmpCoach> atse = service.findAllCoach();
       /* for (AtsEmpCoach coach : atse) {
            System.err.println("================="+coach.getCoachName());
        }*/
        return atse;
    }

    /**
     * 下拉框加载所有项目
     * @return
     */
    @RequestMapping("findAllSport")
    @ResponseBody
    public List<AtsBaseSport> findAllSport(){

        List<AtsBaseSport> sport = service.findAllSport();

        return sport;
    }
    @RequestMapping("loadingResult")
    @ResponseBody
    public List<AtsDataDic> loadingResult(Integer dcode){
        return service.findAllDataDic(dcode);
    }
    /**
     * 加载技术等级选项
     * @return 技术等级
     */
    @RequestMapping("loadingDic")
    @ResponseBody
    public List<AtsDataDic> findAllDataDic(Integer dcode){
        //System.out.println(dcode);
       List<AtsDataDic> data = service.findAllDataDic(dcode);
       /* for (AtsDataDic datum : data) {
            System.out.println("==============="+datum.getDname()+"ID："+datum.getDid());
        }*/
        return data;
    }

    /**
     * 加载文化程度选项
     * @param dcode
     * @return
     */
    @RequestMapping("loadingCulture")
    @ResponseBody
    public List<AtsDataDic> loadingCulture(Integer dcode){
       List<AtsDataDic> data = service.findAllDataDic(dcode);
        /*for (AtsDataDic datum : data) {
            System.out.println(datum.getDname());
        }*/
        return data;
    }
    /**
     * 修改运动员或新增
     * @param ats
     * @return
     */
    @RequestMapping("athleteUpdateOrAdd")
    @ResponseBody
    public Message athleteUpdateOrAdd(AtsEmpAthleteVo ats,MultipartFile file){
        Message msg = new Message();
        //System.err.println("项目ID:"+ats.getSportId()+",教练ID:"+ats.getCoachName());
        int cout =  service.upAthleteDate(ats,file);
        System.out.println("cout:"+cout);
        if(cout>0){
            msg.setMsg("操作成功");
        }else{
            msg.setMsg("操作失败");
        }
        return  msg;

    }

    @RequestMapping("deleteExpertDate")
    @ResponseBody
    public Message deleteAtheleDate(Integer id){
        Message msg = new Message();
        System.out.println(id);
        int cout = service.deleteAthleteDate(id);
        if(cout>0){
            msg.setMsg("删除成功");
        }else {
            msg.setMsg("删除失败");
        }
        return msg;
    }
    /**
     * 绑定教练员手下的运动员
     */
    @RequestMapping("coachAthlete")
    @ResponseBody
    public List<AtsEmpAthlete> coachAthlete(Integer id){
        System.out.println("aaaaaaaaaaaaaaaaa:"+id);
        List<AtsEmpAthlete> list = service.findbycoachId(id);
        return list;
    }

    /**
     * 批量删除
     * @param ids 需要删除的ID
     * @return
     */
    @RequestMapping("deleteAtheleteDates")
    @ResponseBody
    public Message deleteAtheleteDates(String ids){
        Message msg = new Message();//创建msg对象
        //System.out.println("==================="+ids);
        String[] as = ids.split(",");//根据逗号分割字符串入数组
        int cout =0;//声明信息变量
        for (int i = 0; i < as.length; i++) {//开启FOR循环
            //System.out.println(as[i]);
            Integer ahtleteIds = Integer.valueOf(as[i]);//字符串转换Integer
           // System.out.println("====================eeeeeeeee"+e);
          cout = service.deleteAthleteDate(ahtleteIds);//信息变量接收
          //System.out.println(cout);

        }
        System.out.println("cout============"+cout);
        if(cout>0){//根据信息判断
            msg.setMsg("操作成功");
        }else{
            msg.setMsg("操作异常");
        }
       // System.err.println(as.length);
        return  msg;
    }

    /**
     * 根据科研教练员查询运动员  返回list
     * @param oldCoach
     * @return
     */
    @RequestMapping("selectByOldCoach")
    @ResponseBody
    public List<AtsEmpAthlete> selectByOldCoach(Integer oldCoach){
        return service.selectByOldCoach(oldCoach);
    }


}
